<template>
	<view class="page">
		<view class="body">
			<view class="body_title justify-content-sp">
				<view class="body_title_">
					提现账户
				</view>
				<view class="body_title_img" @click="chooseWay">
					<image src="../../static/purse/zhanghuliebiao.png" mode="" class="image"></image>
				</view>
			</view>
			<view class="body_cont" v-if="type == 3">
				<view class="body_cont_text">
					银行账户
				</view>
				<view class="body_cont_text">
					{{bank_name}}
				</view>
				<view class="body_cont_text">
					{{name}}({{maskYhkNumber(account)}})
				</view>
			</view>
			<view class="body_cont" v-if="type == 2">
				<view class="body_cont_text">
					支付宝账户
				</view>
				<view class="body_cont_text">
					{{name}}({{maskPhoneNumber(account)}})
				</view>
			</view>
		</view>
		<view class="body">
			<view class="body_title" style="border: none;">
				<view class="body_title_">
					提现金额
				</view>
				<view class="body_title_money">
					<view class="body_title_money_left">
						￥
					</view>
					<view class="body_title_money_right">
						<input type="number" class="uni-input" placeholder="请输入金额" v-model="money" />
					</view>
				</view>
				<view class="body_title_se">
					当前可用余额{{all_amount}}元，<text style="color: #ff6433;" @click="all">全部提现</text>
				</view>
			</view>
		</view>
		<view class="btn" style="margin-top: 40px;" @click="pay_open">
			确定
		</view>
		<!-- 选择提现账户 -->
		<uni-popup ref="popup_way" type="bottom" border-radius="10px 10px 0 0">
			<view class="popup">
				<view class="">
					<uni-icons type="close" size="30" @click="popup_close"></uni-icons>
				</view>
				<view class="popup_title">
					选择提现账户
				</view>
				<view class="pay_way_list">
					<uni-data-checkbox v-model="value" :localdata="pay_way_List" @change="change"
						:multiple="false"></uni-data-checkbox>
				</view>
				<view class="btn" style="margin-top: 60rpx;" @click="btn">
					确定
				</view>
			</view>
		</uni-popup>
		<!-- 公告弹窗 -->
		<uni-popup ref="announcement" type="bottom" border-radius="10px 10px 0 0">
			<view class="popups">
				<view class="popups_title">
					五一节假日公告
				</view>
				<view class="popups_text">
					尊敬的商家您好
				</view>
				<view class="popups_text">
					关于五一节假日等商家提现到账安排如下：五月一日-五月五日为财务人员放假期间！
				</view>
				<view class="btn btn_css" @click="announcement_close">
					关闭
				</view>
			</view>
		</uni-popup>
		<!-- 支付密码弹窗 -->
		<uni-popup ref="pay_popup" type="bottom" border-radius="10px 10px 0 0">
			<view class="popups">
				<uni-icons type="close" size="30" @click="pay_close(0)"></uni-icons>
				<view class="popups_title" style="margin-top: -30px;color: black;font-size: 25px;">
					请输入支付密码
				</view>
				<view class="popus_body">
					提现
				</view>
				<view class="popus_money justify-content-center">
					<text class="popus_money_right">
						<text style="font-size: 18px;">￥</text>{{money}}
					</text>
				</view>
				<view class="popus_money_fir justify-content-sp">
					<view class="popus_money_fir_left">
						手续费
					</view>
					<view class="popus_money_fir_right">
						￥{{handingfee}}
					</view>
				</view>
				<view class="popus_money_se">
					由银行机构收取相应手续费
				</view>
				<view class="popus_money_thr">
					<input type="password" placeholder="请输入密码" class="password" :maxlength="6" v-model="password">
				</view>
				<view class="btn btn_css" @click="pay_close(1)">
					确定
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	import {
		getBanklist,
		getSubmit,
		getCheckWithdraw
	} from '../../api/payouts/index.js'
	import {
		getIndex
	} from '../../api/index/index.js'
	export default {
		data() {
			return {
				value: 0, //提现方式
				money: '', //提现金额
				payoutsWay: 0, //提现方式
				pay_way_List: [],
				account: '',
				name: '',
				bank_name: '',
				type: '',
				password: '',
				handingfee: '',
				all_amount: ''
			}
		},
		onLoad() {
			this.init();
		},
		methods: {
			all() {
				this.money = this.all_amount
			},
			// 隐藏手机号
			maskPhoneNumber(phoneNumber) {
				if (/^\d{11}$/.test(phoneNumber)) {
					const firstThree = phoneNumber.slice(0, 3);
					const lastFour = phoneNumber.slice(7);
					const maskedMiddle = '****';
					return `${firstThree}${maskedMiddle}${lastFour}`;
				} else {
					throw new Error('Invalid phone number format');
				}
			},
			// 隐藏银行卡号
			maskYhkNumber(yhkNumber) {
				const firstThree = yhkNumber.slice(0, 4);
				const lastFour = yhkNumber.slice(-4);
				const maskedMiddle = ' **** **** ';
				return `${firstThree}${maskedMiddle}${lastFour}`;
			},
			// 获取账户列表
			init() {
				getBanklist().then(res => {
					this.pay_way_List = res.data.bank;
				});
				getIndex().then(res => {
					this.all_amount = res.data.money;
				})
			},
			// 选择提现账户
			chooseWay() {
				this.$refs.popup_way.open('bottom')
			},
			//关闭提现账户
			popup_close() {
				this.$refs.popup_way.close('bottom')
			},
			// 选择提现账户
			btn() {
				if (this.value != 0) {
					this.payoutsWay = this.value;
					for (var i = 0; i < this.pay_way_List.length; i++) {
						if (this.pay_way_List[i].value === this.value) {
							this.account = this.pay_way_List[i].bank_no;
							this.name = this.pay_way_List[i].true_name;
							this.type = this.pay_way_List[i].bank_type;
							this.bank_name = this.pay_way_List[i].bank_name;
						}
					}
				}
				this.$refs.popup_way.close('bottom')
			},
			// 提现方式
			change(e) {
				this.value = e.detail.value;
			},
			// 开启公告弹窗
			announcement() {
				this.$refs.announcement.open('center')
			},
			// 关闭公告弹窗
			announcement_close() {
				this.$refs.announcement.close('center')
			},
			// 支付弹窗
			pay_open() {
				for (var i = 0; i < this.pay_way_List.length; i++) {
					if (this.pay_way_List[i].value === this.value) {
						var account = this.pay_way_List[i].bank_no;
						var name = this.pay_way_List[i].true_name;
						var type = this.pay_way_List[i].bank_type;
						var data = {
							money: this.money,
							account: account,
							name: name,
							type: type,
						};
						getCheckWithdraw(data).then(res => {
							this.handingfee = res.data.handingfee;
							this.$refs.pay_popup.open('center')
						})
					}
				}
			},
			// 关闭支付弹窗
			pay_close(e) {
				if (e == 0) {
					this.$refs.pay_popup.close('center')
				} else {
					var data = {
						money: this.money,
						account: this.account,
						name: this.name,
						type: this.type,
						pwd: this.password
					};
					getSubmit(data).then(res => {
						this.$refs.pay_popup.close('center')
						uni.showToast({
							"icon": "none",
							"title": res.msg
						})
						setTimeout(() => {
							uni.navigateTo({
								url: `/pages/payouts/await?type=1`
							})
						}, 1500);
					})
				}
			},
		}
	}
</script>

<style lang="scss" scoped>
	.page {
		.body {
			.body_title {
				border-bottom: 1px solid #b2b2b2;
				padding: 10px 0;

				.body_title_ {
					font-weight: 600;
				}

				.body_title_money {
					border-bottom: 1px solid #b2b2b2;
					padding: 20rpx 0;
					display: flex;

					.body_title_money_left {
						font-size: 20px;
					}

					.body_title_money_right {
						padding: 5rpx;
					}
				}

				.body_title_se {
					padding: 30rpx 0 0 0;
				}

				.body_title_img {
					width: 16px;
					height: 16px;

					.image {
						width: 100%;
						height: 100%;
					}
				}
			}

			.body_cont {
				padding: 30rpx 0 0 0;

				.body_cont_text {
					line-height: 28px;
					font-size: 17px;
				}
			}
		}

		.popup {
			background-color: white;
			height: 300px;
			border-radius: 30rpx 30rpx 0 0;
			padding: 30rpx;

			.popup_title {
				text-align: center;
				font-size: 20px;
				font-weight: 550;
				margin-top: -20rpx;
			}

			.pay_way_list {
				padding: 30rpx 0;
				overflow: auto;
				height: 120px;
			}
		}

		.popups {
			background-color: white;
			width: 640rpx;
			padding: 30rpx;
			border-radius: 30rpx;

			.popups_title {
				text-align: center;
				font-size: 28px;
				color: #ff4509;
				padding: 30rpx;
			}

			.popus_body {
				margin-top: 20rpx;
				text-align: center;
			}

			.popus_money {
				align-items: flex-end;
				padding-bottom: 20rpx;
				border-bottom: 1px solid #b2b2b2;

				.popus_money_right {
					font-size: 30px;
				}
			}

			.popus_money_fir {
				padding: 30rpx 0;
				font-size: 18px;

				.popus_money_fir_left {
					font-weight: 550;
				}

				.popus_money_fir_right {
					color: red;
				}
			}

			.popus_money_se {
				color: #b2b2b2;
			}

			.popus_money_thr {
				padding: 30rpx;

				.password {
					text-align: center;
					font-size: 40px;
					border: 1px solid #b2b2b2;
					padding: 10rpx;
					border-radius: 15rpx;
				}
			}

			.btn_css {
				padding: 30rpx;
				border-radius: 50rpx;
			}

			.popups_text {
				line-height: 30px;
				font-size: 18px;
			}

			.btn_css {
				margin-top: 30px;
			}
		}
	}
</style>